<?php

namespace App\Http\Controllers;

use App\Datatables;
use App\Postings;
use Illuminate\Http\Request;
use DB;

/**
 * Class PostingsController
 * @package App\Http\Controllers
 */
class PostingsController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * 发帖  old
     * @param Request $request
     * @return array|\Illuminate\Contracts\View\Factory|\Illuminate\Http\RedirectResponse|\Illuminate\View\View
     * @throws \Exception
     */
    public function post(Request $request)
    {
        //如果是post方法为发帖
        if ($request->isMethod('post')) {
            $return = Postings::Post($request);
            if ($return['status'] === 1) {
                return redirect('/postings/postingsList')->with($return);
            } else {
                return back()->with('status', $return['status'])->with('message', $return['message']);
            }


        }

        return view('postings.post');
    }

    /**
     * 帖子列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     */
    public function postingsList(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Datatables::PostingsList());
        }
        return view('postings.postingslist');
    }

    /**
     * 看帖
     * @param $custid
     * @param $page
     * @param Request $request
     * @return $this|array|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector
     */
    public function detail($custid, $page, Request $request)
    {

        //get请求则显示帖子详情
        $tiezi = Postings::PostingDetail($custid);//  Curl::GetCurl(Curl::CURL_ACTLOOK, ['id' => $custid, 'custid' => 30]);
        $huifu = Postings::PostingsDetails($custid, $page);

        //页数
        $pages = ceil($tiezi['ping_num'] / Postings::POSTINGS_HUI);

        return view('postings.postingsDetail')->with(compact('tiezi', 'huifu', 'pages'));
    }

    /**
     * 回帖
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function posting(Request $request)
    {
        if ($request->isMethod('post')) {
            if (!$request->contents) {
                return ['status' => 2, 'message' => '请填写内容'];
            }
            //回帖
            $contents = strip_tags($request->contents);
            $return = Postings::Huitie($request->archive_id, $request->user_id, $contents, $request->view_time);
            return $return;
//            return back()->with('status', $return['status'])->with('message', $return['message']);
        } else {
            return ['status' => 2, 'message' => '回复失败'];
            return back()->with('status', 2)->with('message', '回复失败');
        }
    }


    /**
     * 删除帖子
     * @param $custid
     * @return array|\Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function postingDel($custid, Request $request)
    {
        //是否存在该帖子
        if (!DB::table('tbl_archive')->where('archive_id', $custid)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }
        return response()->json(Postings::PostingDelect($custid, $request->message));

        //删除帖子
//        if (!DB::table('tbl_archive')->where('archive_id', $custid)->delete()) {
//            return ['status' => 2, 'message' => '删除失败'];
//        }
//
//        return ['status' => 1, 'message' => '删除成功'];
    }

    public function postingCancle($custid)
    {
        //是否存在该帖子
        if (!DB::table('tbl_archive')->where('archive_id', $custid)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }
        //删除帖子
        if (!DB::table('tbl_archive')->where('archive_id', $custid)->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }

        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 回复楼层的回复
     * @param Request $request
     * @return array
     */
    public function postingRereply(Request $request)
    {
        if (!$request->contents) {
            return ['status' => 2, 'message' => '请填写内容'];
        }
        return Postings::PostingRereply($request);
    }

    /**
     * 回复楼层
     * @param Request $request
     * @return array
     */
    public function postingRereplys(Request $request)
    {
        if (!$request->contents) {
            return ['status' => 2, 'message' => '请填写内容'];
        }
        return Postings::PostingRereplys($request);
    }

    /**
     * 删除楼层
     * @param $id
     * @return array
     */
    public function postingRereplysDels($id)
    {
        //是否存在
        if (!DB::table('tbl_article_comment')->where('id', $id)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }
        return Postings::DelReply($id);
    }


    /**
     * 删除楼层回复
     * @param $id
     * @return array
     */
    public function postingRereplyDel($id)
    {
        //是否存在
        if (!DB::table('tbl_archive_reply')->where('id', $id)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }

        return Postings::DelPostingReply($id);

    }


    /**
     * 加精
     * @param $id
     * @return array
     */
    public function postingJj($id, Request $request)
    {
        //是否存在该帖子
        if (!DB::table('tbl_archive')->where('archive_id', $id)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }

        return response()->json(Postings::PostingJj($id, $request->time));
//        //加精
//        if (!DB::table('tbl_archive')->where('archive_id', $id)->update(['is_hot' => Postings::HOT_YES])) {
//            return ['status' => 2, 'message' => '失败'];
//        }
//
//        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 取消加精
     * @param $id
     * @return array
     */
    public function postingQjj($id)
    {
        //是否存在该帖子
        if (!DB::table('tbl_archive')->where('archive_id', $id)->exists()) {
            return ['status' => 2, 'message' => '帖子不存在'];
        }

        //取消加精
        if (!DB::table('tbl_archive')->where('archive_id', $id)->update(['is_hot' => Postings::HOT_NO, 'hot_time' => null])) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 修改文章
     * @param Request $request
     * @param Postings $postings
     * @return $this
     */
    public function postEdit(Request $request, $custid)
    {
        if ($request->isMethod('post')) {
            $modify =  Postings::PostEdit($request, $custid);
            return back()->with('status', $modify['status'])->with('message', $modify['message']);
        }
        $tiezi = Postings::PostingDetail($custid);
        return view('postings.postingsEdit')->with(compact('tiezi'));
    }

    /**
     * 删除图片
     * @param $pic_id
     * @return array
     */
    public function picDel($pic_id)
    {
        $art_id = DB::table('tbl_picture')->where('picture_id', $pic_id)->value('archive_id');
        //如果这是最后一张图片
        if (DB::table('tbl_picture')->where('archive_id', $art_id)->count() <= 1) {
            return ['status' => 2, 'message' => '您至少要保留一张图片'];
        }
        //删除
        if (!DB::table('tbl_picture')->where('picture_id', $pic_id)->delete()) {
            return ['status' => 2, 'message' => '删除失败'];
        }
        return ['status' => 1, 'message' => '删除成功'];
    }


    /**
     * 批量加精
     * @param Request $request
     * @return array
     */
    public function postingAlljj(Request $request)
    {
        //加精
        if (!DB::table('tbl_archive')->whereIn('archive_id', array_filter($request->ids))->update(['is_hot' => Postings::HOT_YES, 'hot_time' => date('Y-m-d H:i:s', time())])) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 批量取消加精
     * @param Request $request
     * @return array
     */
    public function postingAllqjj(Request $request)
    {
        if (!DB::table('tbl_archive')->whereIn('archive_id', array_filter($request->ids))->update(['is_hot' => Postings::HOT_NO, 'hot_time' => null])) {
            return ['status' => 2, 'message' => '失败'];
        }

        return ['status' => 1, 'message' => '成功'];
    }


    /**
     * 批量删除
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     * @throws \Exception
     */
    public function postingAlldell(Request $request)
    {
        return response()->json(Postings::PostingDelect(array_filter($request->ids), $request->message));
//        if (!DB::table('tbl_archive')->whereIn('archive_id', array_filter($request->ids))->update(['is_view' => Postings::VIEW_NO])) {
//            return ['status' => 2, 'message' => '失败'];
//        }
//
//        return ['status' => 1, 'message' => '成功'];
    }

    /**
     * 发帖 new
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Http\JsonResponse|\Illuminate\View\View
     * @throws \Exception
     */
    public function postTiming(Request $request)
    {
        //如果是post方法为发帖
        if ($request->isMethod('post')) {
            return response()->json(Postings::Post($request));
        }

        return view('postings.postingTiming');
    }

    public function tongji(Request $request)
    {
        if ($request->isMethod('post')) {
            return response()->json(Postings::PostTongji($request));
        }

        return view('postings.tongji');
    }
}
